package team.paimengshop.server_dataclass;

import org.json.simple.JSONObject;
import team.paimengshop.server_database.CommodityInfoOperate;

import java.util.ArrayList;

public class Commodity
{
    private int id;              //商品编号

    private String name;//商品名称

    private double price;        //商品价格

    private String introduction; //商品简介

    private String imageURL;  //图片路径

    public Commodity()
    {
        this.id = -1;
        this.name = null;
        this.price = -1;
        this.introduction = null;
        this.imageURL = null;
    }

    public Commodity(int id, String name, int price, String introduction, String imageURL, String isRecommend)
    {
        this.id = id;
        this.name = name;
        this.price = price;
        this.introduction = introduction;
        this.imageURL = imageURL;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return name;
    }

    public void setId(int id)
    {
        this.id = id;
    }

    public int getId()
    {
        return id;
    }

    public void setPrice(double price)
    {
        this.price = price;
    }

    public double getPrice()
    {
        return price;
    }

    public void setIntroduction(String introduction)
    {
        this.introduction = introduction;
    }

    public String getIntroduction()
    {
        return introduction;
    }

    public void setImageURL(String imageURL)
    {
        this.imageURL = imageURL;
    }

    public String getImageURL()
    {
        return imageURL;
    }


    //将ArrayList<Commodity>转为commodityList字符串存入数据库
    public static String commodityArrayListToString(ArrayList<Commodity> commodityArrayList)
    {
        String databaseCommodityList = "";
        for(int i = 0; i < commodityArrayList.size(); i++)
        {
            databaseCommodityList += commodityArrayList.get(i).getId();
            databaseCommodityList += ',';
        }
        return databaseCommodityList;
    }

    //把commodityArrayList字符串转为ArrayList<Commodity>
    //数据库中存放的是形如:  12,2,7,45,63,  这样的字符串
    public static ArrayList<Commodity> getCommodityArrayListFromString(String databaseCommodityList)
    {
        ArrayList<Integer> arrayList = new ArrayList<>();
        ArrayList<Commodity> commodityArrayList = new ArrayList<>();
        int id = 0;
        for(int i = 0; i < databaseCommodityList.length(); i++)
        {
            if(databaseCommodityList.charAt(i) != ',')
            {
                id *= 10;
                id += Integer.parseInt(String.valueOf(databaseCommodityList.charAt(i)));
            }
            else
            {
                arrayList.add(id);
                id = 0;
            }
        }
        for(int i = 0; i < arrayList.size(); i++)
        {
            Commodity commodity = CommodityInfoOperate.FindById(arrayList.get(i));
            commodityArrayList.add(commodity);
        }
        return commodityArrayList;
    }

    //将commodity转为Json
    public static JSONObject CommodityToJson(Commodity commodity)
    {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", Integer.toString(commodity.getId()));
        jsonObject.put("name", commodity.getName());
        jsonObject.put("introduction", commodity.getIntroduction());
        jsonObject.put("imageURL", commodity.getImageURL());
        jsonObject.put("price", Double.toString(commodity.getPrice()));
        return jsonObject;
    }
}
